<?php
/* script fills missing quotes into db
 * 
 */

set_include_path(get_include_path(). PATH_SEPARATOR. 'inc/');
require_once 'inc/globals.secret.php';
function __autoload($class_name) {
  require_once $class_name.'.class.php';
}

if (!array_key_exists (1,$argv) || in_array ("-h", $argv)) {
  echo "usage:   php ".$_SERVER["PHP_SELF"]." <parameters>\n";
  echo "example: php ".$_SERVER["PHP_SELF"]." -h\n";  
  echo "Parameters:\n";
  echo "   --showdatabase       : show first and last dates in DB\n";
  echo "   --showfiles          : show first and last dates of eod data files\n";
  echo "   --showallfiles       : show all available eod data files\n";
  echo "   --showmissing        : show files that are not included in DB\n";
  echo "   --showoverlapping    : show files that are to written to DB again\n";  
  echo "   --insertoverlapping  : insert/update overlapping files into DB\n";
  echo "   --insertmissing      : insert missing files into DB\n";
  echo "\n";
  die;
}

$db = new db($dbparams["host"], $dbparams["user"], $dbparams["password"]);
$db->connect("va_stratlab");

$FDB = new fileToDb($db, $eoddatadir); 

if (in_array("--showdatabase", $argv)) {
  echo "last date in database:\n";
  foreach ($FDB->dbinfo as $key=>$value) {
    echo "  ".$value['first']." ".$value['last']." (".$key.") (".$FDB->refsyms[$key].")\n";
  }
}

if (in_array("--showfiles", $argv)) {
  echo "available eod data files (range):\n";
  foreach ($FDB->fileinfo as $key=>$value) {
    sort($value);
    echo "   ".$value[0]." ".$value[sizeof($value)-1]." (".$key.")\n";
  }
}

if (in_array("--showallfiles", $argv)) {
  echo "all available eod data files:\n";
  foreach ($FDB->fileinfo as $key=>$value) {
    echo $key.":\n";
    foreach ($value as $dt) {
      echo $dt." ";
    }
    echo "\n";
  }  
}

if (in_array("--showmissing", $argv)) {
  echo "files that are not yet included in DB:\n";
  foreach ($FDB->missingInDb as $key=>$value) {
    echo $key.":\n";
    foreach ($value as $dt) {
      echo $dt." ";
    }
    echo "\n";    
  }
}

if (in_array("--showoverlapping", $argv)) {
  echo "files that are to be written into DB again:\n";
  foreach ($FDB->overlapping as $key=>$value) {
    echo $key.":\n";
    foreach ($value as $dt) {
      echo $dt." ";
    }
    echo "\n";    
  }
}

if (in_array("--insertoverlapping", $argv)) {
  echo "now inserting / updating overlapping files into DB\n";
  $FDB->insertMissing("overlapping", "replace");
}

if (in_array("--insertmissing", $argv)) {
  echo "now inserting missing files into DB\n";
  $FDB->insertMissing("missing", "noreplace");
}

$db->disconnect();
?>
